package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.DishFlavor;
import com.langhua.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dishFlavors")
public class DishFlavorController {

    @Autowired
    private DishFlavorService dishFlavorService;



    @GetMapping
    public R<List<DishFlavor>> getPage(){
        System.out.println("菜品分类全部查询：");

        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<DishFlavor>();
        List<DishFlavor> list = dishFlavorService.list(lqw);
        System.out.println("菜品全部查询内容："+list);
        return R.success(list,"dishFlavor全部查询查询");
    }

    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current,@PathVariable Integer size,DishFlavor dishFlavor){
        System.out.println("菜品分类模糊查询："+dishFlavor.getFlavorType());

        IPage page=new Page(current,size);
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<DishFlavor>();
        lqw.like(Strings.isNotEmpty(dishFlavor.getFlavorType()),DishFlavor::getFlavorType,dishFlavor.getFlavorType());

        IPage page1 = dishFlavorService.page(page,lqw);
        System.out.println("菜品分类分页查询内容："+page1);
        return R.success(page1,"dishFlavor分页查询");
    }
    /**
     * 新增分类
     * @param dishFlavor
     * @return
     */
    @PostMapping("/add")
    public R<String> save(HttpServletRequest request,@RequestBody DishFlavor dishFlavor){
        log.info("新增分类，分类信息：{}",dishFlavor.toString());


        System.out.println("新增得id："+dishFlavor);
        dishFlavorService.save(dishFlavor);

        return R.success(null,"新增分类成功");
    }


    /**
     * 根据id查询员工信息 (修改数据回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishFlavor> getById(@PathVariable Long id){
        log.info("根据id查询员工信息..."+id);
        DishFlavor dishFlavor = dishFlavorService.getById(id);
        if(dishFlavor != null){
            return R.success(dishFlavor,null);
        }
        return R.error("没有查询到对应员工信息");
    }

    /**
     * 根据id修改员工信息()
     * @param dishFlavor
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody DishFlavor dishFlavor){
        log.info(dishFlavor.toString());

        long id = Thread.currentThread().getId();
        log.info("修改id为：{}",id);
        //Long empId = (Long)request.getSession().getAttribute("dishFlavor");
        //dishFlavor.setUpdateTime(LocalDateTime.now());
        //dishFlavor.setUpdateUser(empId);

        dishFlavorService.updateById(dishFlavor);

        return R.success(null,"分类信息修改成功");
    }

    /**
     * 通过id删除DishFlavor
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除DishFlavor的id为：{}", id);

        dishFlavorService.removeById(id);
        return R.success(null,"删除成功！");
    }

}
